package com.chenxi.vhr.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : zhangchenxi
 * @create : 2022-02-12
 **/
public class StringUtil {

    /**
     * 判空
     * @param in
     * @return
     */
    public static boolean isNull(String in)
    {
        return (in == null) || ("".equals(in.trim()));
    }

    public static boolean isDecimal(String num)
    {
        try
        {
            new BigDecimal(num);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    public static BigDecimal asDecimal(String s)
    {
        return asDecimal(s, true);
    }

    public static BigDecimal asDecimal(String s, boolean nullable)
    {
        BigDecimal rtn = null;
        if ((!isNull(s)) && (isDecimal(s))) {
            rtn = new BigDecimal(s);
        }
        if ((!nullable) && (rtn == null)) {
            rtn = new BigDecimal(0);
        }
        return rtn;
    }

    public static double asDouble(BigDecimal in)
    {
        if (in == null)
            return 0.0D;
        return in.doubleValue();
    }

    public static int asInt(BigDecimal b)
    {
        Double d = Double.valueOf(asDouble(b));
        return d.intValue();
    }

    public static int asInt(Integer i)
    {
        if (i == null)
            return 0;
        return i.intValue();
    }

    public static int asInt(Long l)
    {
        if (l == null)
            return 0;
        return asInt(l.toString());
    }

    public static int asInt(String s)
    {
        if (isNull(s)) {
            return 0;
        }
        return asInt(new BigDecimal(s));
    }

    public static long asLong(Long l)
    {
        if (l == null) {
            return 0L;
        }
        return l.longValue();
    }


    public static String asString(BigDecimal b, int scale)
    {
        return asString(b, scale, false);
    }

    public static String asString(BigDecimal b, int scale, boolean strpTrailingZeros)
    {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        if (b == null)
            return "0";
        if (strpTrailingZeros) {
            if (asDouble(b) == 0.0D) {
                return "0";
            }
            return b.setScale(scale, 4).stripTrailingZeros().toPlainString();
        }
        return b.setScale(scale, 4).toPlainString();
    }

    public static String asString(Date in, String format)
    {
        if (in == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(in);
    }

    public static String asString(Integer i)
    {
        if (i == null)
            return "";
        return String.valueOf(i);
    }

    public static String asString(Long l)
    {
        if (l == null)
            return "";
        return String.valueOf(l);
    }


    public static Integer asInt(Double d)
    {
        if (d == null)
            return 0;
        return d.intValue();
    }



}
